use image::{DynamicImage, ImageError, Rgba, RgbaImage};
use std::path::Path;

pub struct ImageUtil;

impl ImageUtil {
    /// 读取图像文件并返回图像对象
    pub fn read_image<P: AsRef<Path>>(path: P) -> Result<DynamicImage, ImageError> {
        image::open(path)
    }

    /// 保存图像对象到文件
    pub fn save_image<P: AsRef<Path>>(image: &DynamicImage, path: P) -> Result<(), ImageError> {
        image.save(path)
    }

    /// 缩放图像到指定的宽度和高度
    pub fn resize_image(image: &DynamicImage, width: u32, height: u32) -> RgbaImage {
        image
            .resize_exact(width, height, image::imageops::FilterType::Lanczos3)
            .to_rgba8()
    }

    /// 裁剪图像到指定的矩形区域
    pub fn crop_image(image: &DynamicImage, x: u32, y: u32, width: u32, height: u32) -> RgbaImage {
        image.crop_imm(x, y, width, height).to_rgba8()
    }

    /// 旋转图像 90 度
    pub fn rotate90(image: &DynamicImage) -> RgbaImage {
        image.rotate90().to_rgba8()
    }

    /// 旋转图像 180 度
    pub fn rotate180(image: &DynamicImage) -> RgbaImage {
        image.rotate180().to_rgba8()
    }

    /// 旋转图像 270 度
    pub fn rotate270(image: &DynamicImage) -> RgbaImage {
        image.rotate270().to_rgba8()
    }

    /// 将图像转换为灰度图像
    pub fn to_grayscale(image: &DynamicImage) -> RgbaImage {
        image.grayscale().to_rgba8()
    }

    /// 在图像上绘制矩形
    pub fn draw_rectangle(
        image: &mut RgbaImage,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
        color: Rgba<u8>,
    ) {
        for i in x..x + width {
            for j in y..y + height {
                if i < image.width() && j < image.height() {
                    image.put_pixel(i, j, color);
                }
            }
        }
    }

    /// 生成缩略图，保持原图像比例
    ///
    /// # 参数
    /// * `image` - 原图像对象
    /// * `max_dimension` - 缩略图的最大边长
    ///
    /// # 返回值
    /// 返回生成的缩略图图像对象
    pub fn generate_thumbnail(image: &DynamicImage, max_dimension: u32) -> RgbaImage {
        image.thumbnail(max_dimension, max_dimension).to_rgba8()
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let img_path = "input.png";
    let output_path = "output.png";

    // 读取图像
    let image = ImageUtil::read_image(img_path)?;

    // 缩放图像
    let resized_image = ImageUtil::resize_image(&image, 200, 200);
    ImageUtil::save_image(&DynamicImage::ImageRgba8(resized_image), "resized.png")?;

    // 裁剪图像
    let cropped_image = ImageUtil::crop_image(&image, 50, 50, 100, 100);
    ImageUtil::save_image(&DynamicImage::ImageRgba8(cropped_image), "cropped.png")?;

    // 旋转图像
    let rotated_image = ImageUtil::rotate90(&image);
    ImageUtil::save_image(&DynamicImage::ImageRgba8(rotated_image), "rotated.png")?;

    // 转换为灰度图像
    let grayscale_image = ImageUtil::to_grayscale(&image);
    ImageUtil::save_image(&DynamicImage::ImageRgba8(grayscale_image), "grayscale.png")?;

    // 在图像上绘制矩形
    let mut image_with_rectangle = image.to_rgba8();
    ImageUtil::draw_rectangle(&mut image_with_rectangle, 10, 10, 50, 50, Rgba([255, 0, 0, 255]));
    ImageUtil::save_image(&DynamicImage::ImageRgba8(image_with_rectangle), "rectangle.png")?;

    // 生成缩略图
    let thumbnail = ImageUtil::generate_thumbnail(&image, 100);
    ImageUtil::save_image(&DynamicImage::ImageRgba8(thumbnail), "thumbnail.png")?;

    Ok(())
}